﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace MapApp
{
    //Сюда засунь все фильтры
    //Signature  - private delegate Bitmap ProcessImageFunc(Bitmap pic);
    class MyFilters
    {
        public static Bitmap Filter(Bitmap pic, int[,] mtrx, int offset) {
            Bitmap tmp = pic;
            Bitmap NewBitmap = new Bitmap(tmp.Width, tmp.Height);
            int width = mtrx.Rank + 1, height = mtrx.Rank + 1;

            Graphics NewGraphics = Graphics.FromImage(NewBitmap);
            NewGraphics.DrawImage(tmp, new Rectangle(0, 0, tmp.Width, tmp.Height), new Rectangle(0, 0, tmp.Width, tmp.Height), GraphicsUnit.Pixel);
            NewGraphics.Dispose();
            for (int x = 0; x < pic.Width; ++x)
            {
                for (int y = 0; y < pic.Height; ++y)
                {
                    int rValue = 0;
                    int gValue = 0;
                    int bValue = 0;
                    int weight = 0;
                    int xCurrent = -width / 2;
                    for (int x2 = 0; x2 < width; ++x2)
                    {
                        if (xCurrent + x < pic.Width && xCurrent + x >= 0)
                        {
                            int YCurrent = -height / 2;
                            for (int y2 = 0; y2 < height; ++y2)
                            {
                                if (YCurrent + y < pic.Height && YCurrent + y >= 0)
                                {
                                    rValue += mtrx[x2, y2] * tmp.GetPixel(xCurrent + x, YCurrent + y).R;
                                    gValue += mtrx[x2, y2] * tmp.GetPixel(xCurrent + x, YCurrent + y).G;
                                    bValue += mtrx[x2, y2] * tmp.GetPixel(xCurrent + x, YCurrent + y).B;
                                    weight += mtrx[x2, y2];
                                }
                                ++YCurrent;
                            }
                        }
                        ++xCurrent;
                    }
                    Color meanPixel = tmp.GetPixel(x, y);
                    if (weight == 0)
                        weight = 1;
                    if (weight > 0)
                    {
                        rValue = (rValue / weight) + offset;
                        if (rValue < 0)
                            rValue = 0;
                        else if (rValue > 255)
                            rValue = 255;
                        gValue = (gValue / weight) + offset;
                        if (gValue < 0)
                            gValue = 0;
                        else if (gValue > 255)
                            gValue = 255;
                        bValue = (bValue / weight) + offset;
                        if (bValue < 0)
                            bValue = 0;
                        else if (bValue > 255)
                            bValue = 255;
                        meanPixel = Color.FromArgb(rValue, gValue, bValue);
                    }
                    NewBitmap.SetPixel(x, y, meanPixel);
                }
            }
            return NewBitmap;
        }

        public static Bitmap negativeFilter(Bitmap pic, int[,] mtrx, int offset)
        {
            Bitmap newBitmap = new Bitmap(pic);
            for (int i = 0; i < newBitmap.Width; i++)
            {
                for (int j = 0; j < newBitmap.Height; j++)
                {
                    newBitmap.SetPixel(i, j, Color.FromArgb(255 - newBitmap.GetPixel(i, j).R, 255 - newBitmap.GetPixel(i, j).G, 255 - newBitmap.GetPixel(i,j).B));
                }
            }
            return newBitmap;
        }
    }
               
}
